/*************************************************
Author: will
Date: 10/15/2012
Description:
Notes:
*************************************************/

#ifndef __CStorage_h__
#define __CStorage_h__

#include "CTHashObjectMgr.h"
#include "commclass.h"

class CStorageKey       //用于定位确定的格子，VALUE是CStorageGrid
{
public:
    unsigned char _ucItemType;
    unsigned short _usStorePos;
    bool          _bIsInBag;        //在背包中为真, 在仓库中为假
    unsigned int  _uiItemID;        //装备:存放装备ID,  其它的存放配置ID

    CStorageKey()
    {
	    Clear();

    }
	void Clear()
	{
        _bIsInBag = true;
        _ucItemType = 0;
        _usStorePos = STORAGE_POS_NOT_OCCUPIED;
        _uiItemID = 0;
	}
    bool operator==(const CStorageKey& stKey) const
    {
        return (_bIsInBag == stKey._bIsInBag && _ucItemType == stKey._ucItemType && _usStorePos == stKey._usStorePos && _uiItemID == stKey._uiItemID);
    }
};

class CStorageItemGroupKey //用于定位一类物品的格子，VALUE是CStorageGrid数组
{
public:
    unsigned char _ucItemType;
    unsigned int  _uiItemCfgID;

    CStorageItemGroupKey()
    {
        _ucItemType = 0;
        _uiItemCfgID = 0;
    }

	void Init(unsigned char ucItemType, unsigned int uiItemId);

    bool operator==(const CStorageItemGroupKey& stKey) const
    {
        return (_ucItemType == stKey._ucItemType && _uiItemCfgID == stKey._uiItemCfgID);
    }
};

namespace __gnu_cxx  //
{
    template<> struct hash<CStorageKey>
    {
        size_t operator()(const CStorageKey& x) const
        {
            return (x._ucItemType * 100000 + x._uiItemID) * x._usStorePos;
        }
    };

    template<> struct hash<CStorageItemGroupKey>
    {
        size_t operator()(const CStorageItemGroupKey& x) const
        {
            return x._ucItemType * 100000 + x._uiItemCfgID;
        }
    };
}

class CStorageGrid
{
public:
	CStorageGrid()
    {
    	Clear();
	}

	void Clear()
	{
		_stKey.Clear();
        _iPosValue = 0;
        _shCount = 0;
		_shMaxPerGrid = 0;
        _iInvalidTime = 0;
	}

	//是否为空格子
	bool IsEmpty()
	{
		if (0 == _shCount)
		{
			return true;
		}

		return false;
	}

    bool IsValidGrid ();

	int Init (unsigned int uiItemType, unsigned int uiItemID);


	virtual ~CStorageGrid()
    {
	}

public:
    CStorageKey   _stKey;

    int          _iPosValue;        //排序权值，越大排得越前面
    int        _shMaxPerGrid;     //堆叠上限
    int        _shCount;			//堆叠数目
    int         _iInvalidTime;      //物品失效时间
};

typedef CTLib::CTHashObjectMgr<CStorageKey, CStorageGrid> T_STORAGE_HASH;
typedef CTLib::CTHashObjectMgr<CStorageItemGroupKey, int> T_STORAGE_ITEM_TYPE_HASH;
class CStorageHash : public T_STORAGE_HASH
{
public:
    enum
    {
        ITEM_TYPE_INDEX = 2,     //用来记录一类物品的类型
    };

	CStorageHash()
    {
	}

	virtual ~CStorageHash()
    {
	}

    void Clear();

    static size_t CountSize(int iObjectCount);

    int Init(const char* pBuffer, int iObjectCount, bool bResetShm = true);

    virtual int HashAlloc(const CStorageKey& stKey, bool bFindExist = true);
    int HashFind(const CStorageKey& stKey);
    int HashFind(const CStorageItemGroupKey& stKey);
    int FindNextSameTypeItem(int iIndex);
    virtual int HashFreeByIndex(int i);

private:
    T_STORAGE_ITEM_TYPE_HASH    _stItemTypeHash;        //用于存储一类物品的开始下标
};


#endif // __CStorage_h__

